<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1400" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">

					<el-form-item style="margin-top: 18px;" label="所属分类" prop="category_id">
						<blCategory style="width: 400px;" v-model="form.category_id" model="Frp" />
					</el-form-item>
					<el-form-item label="节点名称" prop="frp_name">
						<el-input v-model="form.frp_name" style="width: 400px;"></el-input>
					</el-form-item>
					<el-form-item label="备注信息" prop="description">
						<el-input type="textarea" v-model="form.description" :rows="4" placeholder="请输入内容"></el-input>
					</el-form-item>
					<el-form-item label="frp配置" prop="frp_ini">
						<sc-form-table ref="table" v-model="form.frp_ini" :addTemplate="addTemplate" drag-sort
							placeholder="暂无数据">
							<el-table-column label="名称" prop="name" width="150">
								<template #default="{row}">
									<el-input v-model="row.name" placeholder="请输入字段名称"></el-input>
								</template>
							</el-table-column>
							<el-table-column label="标示" prop="sulg" width="150">
								<template #default="{row}">
									<el-input v-model="row.sulg" placeholder="请输入字段名称"></el-input>
								</template>
							</el-table-column>
							<el-table-column label="类型" prop="type" width="150">
								<template #default="{row}">
									<el-select v-model="row.type" placeholder="请选择">
										<el-option label="http" value="http"></el-option>
										<el-option label="tcp" value="tcp"></el-option>
									</el-select>
								</template>
							</el-table-column>
							<el-table-column label="描述" prop="comment" width="250">
								<template #default="{row}">
									<el-input placeholder="请输入中文描述" v-model="row.comment"></el-input>
								</template>
							</el-table-column>
							<el-table-column label="是否必填" prop="required" width="150">
								<template #default="{row}">
									<el-switch v-model="row.required" :active-value="true"
										:inactive-value="false"></el-switch>
								</template>
							</el-table-column>
						</sc-form-table>
					</el-form-item>
					<div style="height:400px">
						&nbsp;
					</div>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	var _this;

	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				addTemplate: {
					key: '',
					comment: '',
					type: 'string',
					required: true
				},
				shortcuts: [{
					text: "每天0点5分",
					value: "0 5 0 * * ?"
				}],
				activeNames: ['基础设置', '高级设置'],
				props: {
					label: 'title',
					value: 'id',
					keyword: 'keyword'
				},
				activeTab: 'basic',
				data: {

				},
				loading: false,
				mode: "add",
				titleMap: {
					add: '添加frp节点',
					edit: '编辑frp节点'
				},
				form: {
					id: '',
					table: "",
					category_id: null,
					description: "",
					name: ""
				},
				rules: {
					table: [{
						required: true,
						message: '请填写表标识'
					}],
					name: [{
						required: true,
						message: '请填写表名称'
					}],
					category_id: [{
						required: true,
						message: '请选择归属醒目'
					}]
				},
				visible: false,
				isSaveing: false,
				apiObj: null,
			}
		},
		mounted() {
			_this = this;
		},
		methods: {
			getNextCronTime(str) {
				// return getNextCronTime(str);
			},
			success(res) {
				return true;
			},
			//显示
			open(data) {
				if (data) {
					this.mode = data;
				}
				this.visible = true;
				this.$nextTick(() => {
					if (this.mode == 'add') {
						this.$refs.table.rowAdd()
					}
				})
				return this;
			},
			//表单提交方法
			submit() {
				this.$refs.ruleForm.validate(async (data) => {
					if (data) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'add') {
							res = await _this.$API.system.cdp.eventTable.create.post(this.form);
						} else {
							res = await _this.$API.system.cdp.eventTable.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', data, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true

				if (!data.id) {
					if (data.category_id) {
						this.form.category_id = data.category_id
					}
					this.loading = false
					return;
				}

				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.cdp.eventTable.show.get(params)
					this.loading = false
					this.form = res.data
				}, 0)
			}
		}
	}
</script>

<style>
	.custom-empty {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #8c939d;
		border-radius: 5px;
	}

	.custom-empty i {
		font-size: 30px;
		color: #fff;
	}

	.custom-empty p {
		font-size: 12px;
		font-weight: normal;
		color: #fff;
		margin-top: 10px;
	}

	.class_title {
		padding-left: 18px;
		font-size: 18px;
		background-color: #f4f9ff;
		text-align: left;
		width: 100%;
	}

	.section-title {
		font-size: 14px;
		font-weight: 400;
		border-left: 5px solid var(--el-color-primary);
		padding-left: 10px;
		color: var(--el-text-color-regular);
	}
</style>
